<?php
namespace Admin\Controller;
use Think\Controller;

class AuthManageController extends Controller{
    public function GroupList(){
        $map['id']=array('not in','1');
        $list=M('auth_group')->where($map)->select();
        $this->assign('groupList',$list);
        $this->display('AuthManage/grouplist');
    }
    
    public function addGroup(){
        if(IS_POST){
            $title=I('POST.name');
            $status=I('post.status');
            $data['title']=$title;
            $data['status']=$status;
            if($title=='')$this->error('用户组名字不能为空');
            else M('auth_group')->add($data)?$this->success('添加成功',U('GroupList')):$this->error('添加失败');
        }else{
            $this->display('AuthManage/addGroup');
        }
    }
    
    public function editGroupStatus(){
        if(IS_GET){
            $id=I('get.id');
            if($id!=''){
                $status=M('auth_group')->where(['id'=>$id])->field('status')->find();
                $this->assign('status',$status['status']);
                $this->assign('id',$id);
                $this->display('AuthManage/editGroupStatus');
            }
        }else{
            $status=I('post.status');
            $id=I('post.id');
            $data['status']=$status;
            $re=M('auth_group')->where(['id'=>$id])->save($data);
            if($re>=0){
                $this->success('状态修改成功',U('GroupList'));
            }else{
                $this->success('状态修改失败');
            }
        }
    }
    
    public function deleteSingle(){
      $id=I('get.id');
            if($id!=''){
                $re=M('auth_group')->where(['id'=>$id])->delete();
                if($re)  $this->ajaxReturn(true);
                else $this->ajaxReturn(false);
            }
    }
    
    public function authList(){           //显示该组用户所具有的大的功能模块
        if(IS_POST){
           $newRule=I('post.rules');
           $groupid=I('post.groupid');
           $str='';
           foreach ($newRule as $k=>$v){
             $str=$str.$newRule[$k].',';
           }
           $data['rules']=$str;
           $num=M('auth_group')->where(['id'=>$groupid])->save($data);
           $num?$this->success('修改成功',U('GroupList')):$this->error('没有任何改动');
        }else{
            $id=I('get.id');
            $auth=explode(",",M('auth_group')->where(['id'=>$id])->getField('rules'));
            $re=M('auth_rule')->field('id,pid,title')->select();
            foreach($re as $k=>$v){
             $v['check']=in_array($v['id'],$auth) ? 1 : 0;
             $re[$k]=$v;
            }
            $re=list_to_tree($re);
            $this->assign('groupid',$id);
            $this->assign('result',$re);
//             print_r($re);
            $this->display('AuthManage/authList');
        }
    }
    
    public function memberList(){
        $id=I('get.id');
        $list=M('auth_group_access')->join('bls_admin ON bls_admin.id=bls_auth_group_access.uid')->field('bls_auth_group_access.uid,username,email,phone,status')->where(['bls_auth_group_access.group_id'=>$id])->select();
        
        $this->assign('group_id',$id);
        $this->assign('memberList',$list);
        $this->display();
    }
    
    public function deleteMember(){
        $id=I('get.id');
        $re=M('admin')->where(['id'=>$id])->delete();
        $re?$this->success('删除成功'):$this->error('删除失败');
    }
    
    public function editMemberStatus(){
        if(IS_GET){
            $id=I('get.id');
            $re=M('admin')->where(['id'=>$id])->getField('status');
            $this->assign('group_id',I('get.group_id'));
            $this->assign('id',$id);
            $this->assign('status',$re);
            $this->display();
        }else{
            $status=I('post.status');
            $group_id=I('post.group_id');
            $id=I('post.id');
            $data['status']=$status;
            $re=M('admin')->where(['id'=>$id])->save($data);
            $re?$this->success('状态修改成功',U('memberList',['id'=>$group_id])):$this->error('未更改任何数据');
        }
    }
    
    public function Addmember(){
        if(IS_GET){
        $group_id=I('get.group_id');
        $this->assign('group_id',$group_id);
        $this->display();
        }else{
            $name=I('post.name');
            $password=md5(md5(I('post.password')));
            $email=I('post.email');
            $phone=I('post.phone');
            $reg_time=time();
            $group_id=I('post.group_id');
            $data['status']=I('post.status');
            $data['username']=$name;
            $data['password']=$password;
            $data['email']=$email;
            $data['phone']=$phone;
            $data['addtime']=$reg_time;
            $data=D('admin')->create($data);
            $id=($data?D('admin')->add():false);
            if($id){
                $newData['uid']=$id;
                $newData['group_id']=$group_id;
                M('auth_group_access')->add($newData)?$this->success('添加成功',U('memberList',['id'=>$group_id])):$this->error('添加失败');
            }else{
                $this->error(D('admin')->getError());
            }
        }
    }
    
}